ct_table <-
function(data, type = c('total', 'home', 'away'), separator = '-') {
    type <- match.arg(type)
    n <- nrow(data)

    ct_table_names <- c('CLUB',
                        'P',
                        'PT',
                        'W',
                        'D',
                        'L',
                        'GF',
                        'GA',
                        'GD',
                        'PTS',
                        'PDF',
                        'PDL')

    width <- length(ct_table_names)
    ct_table <- as.data.frame(matrix(ncol = width, nrow = 0))
    colnames(ct_table) <- ct_table_names

    ct_table_inner <- function(type_inner) {
        for (j in 1:n) {
            if (type_inner == 'home') {
                element = data[i, j]
            } else {
                element = data[j, i]
            }

            if (is.na(element)) {
                next
            }

            total$P <- total$P + 1
            result <- as.numeric(strsplit(element, separator)[[1]])

            if (type_inner == 'home') {
                gf <- result[1]
                ga <- result[2]
            } else {
                gf <- result[2]
                ga <- result[1]
            }

            total$GF <- total$GF + gf
            total$GA <- total$GA + ga

            if (gf > ga) {
                total$W <- total$W + 1
                total$PTS <- total$PTS + 3
            } else if (gf == ga) {
                total$D <- total$D + 1
                total$PTS <- total$PTS + 1
            } else {
                total$L <- total$L + 1
            }
        }

        return(total)
    }

    for (i in 1:n) {
        total <- as.list(vector(length = width))
        names(total) <- ct_table_names

        if (type == 'total' | type == 'home') {
            total <- ct_table_inner('home')
        }

        if (type == 'total' | type == 'away') {
            total <- ct_table_inner('away')
        }

        total$CLUB <- rownames(data)[i]
        total$GD <- total$GF - total$GA

        ct_table[nrow(ct_table) + 1,] <- total
    }

    ct_table <- ct_table[order(ct_table[, 'PTS'],
                               ct_table[, 'GD'],
                               decreasing = TRUE),]
    rownames(ct_table) <- 1:n

    ct_table[, 'PT'] <- (n - 1) * 2
    ct_table[, 'PDF'] <- ct_table[, 'PTS'] - ct_table[1, 'PTS']
    ct_table[, 'PDL'] <- ct_table[, 'PTS'] - ct_table[n, 'PTS']

    return(ct_table)
}
